* Get Gutierrez/Philippon industry-level data
* the following taken from Baqaee/Fahri (2020) who base it on Gutierrez/Philippon
* see https://academic.oup.com/qje/article-abstract/135/1/105/5573281?redirectedFrom=fulltext
* =================================================================================================

use ${dirInput}/firmOut_Markups_IndERP.dta, clear 

keep ///
beacode ind_all year ///
erp_damfcf r10 bondspread tbond_dam ///
a1_de ///
a1_depk_all_bea a1_g_relp_i ///
mixrate_dam

bysort beacode year: keep if _n==1 
drop if beacode == .
drop if beacode == 521 // "Federal reserve"

la var erp_damfcf "Equity risk premium (ERP) from Damodaran"
la var r10 "Real 10-year Treasury Rate"
la var bondspread "BAA - 10y Treasury spread (r_baa - r10)"
la var tbond_dam "T.Bond Rate from Damodaran"
la var a1_de "industry specific leverage ratio computed from compustat by Baqaee/Fahri (a1_dltt/(a1_be+a1_dltt))"
la var mixrate_dam "Required return on capital (a1_de * (r10 + bondspread) + (1-a1_de)*(erp_damfcf + tbond_dam))"
la var a1_g_relp_i "Growth in real price of investment"

gen rk = mixrate_dam - (1-a1_depk_all_bea)* a1_g_relp_i
la var rk "BF-replicated return on capital (mixrate_dam - (1-a1_depk_all_bea)* a1_g_relp_i)"

sort beacode year
order beacode year ind_all

drop if year<1994
cap drop nyears
bysort beacode: gen nyears=_N
tab nyears
tab ind_all if nyears<21
drop if nyears<21

save ${dirTemp}/BF_industry_data.dta, replace
